package cn.wsalix.admin.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import cn.anlaser.repository.CommonRepository;
import cn.wsalix.admin.entity.SysMenu;

public interface MenuRepository extends CommonRepository<SysMenu> {

	@Query("select u from SysMenu u where u.parent.id = :parentId")
	List<SysMenu> findByParent(@Param("parentId") long parentId);

	@Query("select u from SysMenu u where u.parent is null")
	List<SysMenu> findByParent();

/*	@Query("select u from SysMenu u,RoleMenu rm,UserRole ur where u.id = rm.menu.id and rm.role.id=ur.role.id and ur.user.id=:userId order by u.createdDate")
	List<SysMenu> findByUserId(@Param("userId") Long userId);
	
	@Query("select u from SysMenu u,RoleMenu rm,UserRole ur where u.parent is null and u.id = rm.menu.id and rm.role.id=ur.role.id and ur.user.id=:userId order by u.createdDate")
	List<SysMenu> findParentNullByUserId(@Param("userId") Long userId);

	
	@Query("select u from SysMenu u,RoleMenu rm,UserRole ur where u.parent.id=:parentId and u.id = rm.menu.id and rm.role.id=ur.role.id and ur.user.id=:userId order by u.createdDate")
	List<SysMenu> findByUserIdAndParentId(@Param("userId") Long userId,
			@Param("parentId") Long parentId);

*/	
	@Query(nativeQuery = true,value = "select menu.* from (select admin_menu.*,rolemenu.prefix_id as role_menu from WOW_ADMIN_MENU admin_menu    left outer join (select rm.* from WOW_ADMIN_ROLE_MENU rm  where rm.prefix_id = ?1) rolemenu on admin_menu.id= rolemenu.postfix_id) menu    where role_menu is null")
	public List<SysMenu> findUnMenusByRoleId(@Param("roleId") Long roleId);

	/*@Query("select m from RoleMenu rm,SysMenu m where rm.menu.id = m.id and rm.role.id=:roleId")
	public List<SysMenu> findMenusByRoleId(@Param("roleId") Long roleId);

	@Query("select m from UserRole ur,SysMenu m,RoleMenu rm where ur.role.id = rm.role.id and m.id= rm.menu.id and ur.user.id=:userId")
	public List<SysMenu> findMenusByUserId(@Param("userId") Long userId);
*/
	@Query("select m from SysMenu m where m.code =:code")
	public List<SysMenu> findsByCode(@Param("code") String code);
	
	@Query("select m from SysMenu m where m.name =:name")
	public List<SysMenu> findByName(@Param("name") String name);

	@Query("select m from SysMenu m where m.pattern =:pattern")
	public List<SysMenu> findByPattern(@Param("pattern")String pattern);

	
}
